package com.gouwudai.table.view.style1;


	/*
	 * Copyright 2004 original author or authors.
	 *
	 * Licensed under the Apache License, Version 2.0 (the "License");
	 * you may not use this file except in compliance with the License.
	 * You may obtain a copy of the License at
	 *
	 *    http://www.apache.org/licenses/LICENSE-2.0
	 *
	 * Unless required by applicable law or agreed to in writing, software
	 * distributed under the License is distributed on an "AS IS" BASIS,
	 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	 * See the License for the specific language governing permissions and
	 * limitations under the License.
	 */

import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.extremecomponents.table.bean.Column;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.view.html.BuilderConstants;
import org.extremecomponents.table.view.html.BuilderUtils;
import org.extremecomponents.table.view.html.TableBuilder;
import org.extremecomponents.util.HtmlBuilder;

	/**
	 * @author Jeff Johnston
	 */
public class UserTableBuilder extends TableBuilder{
		public UserTableBuilder(TableModel model) {
	        super(model);
	    }
	    public UserTableBuilder(HtmlBuilder html, TableModel model) {
	        super(html,model);
	    }
		

	    public void headerRow() {
	        getHtmlBuilder().tr(1).close();

	        List columns = this.getTableModel().getColumnHandler().getHeaderColumns();
	        //head重複次數
	        for(int x = 0;x<getMultiColumn();x++){
	        	for (Iterator iter = columns.iterator(); iter.hasNext();) {
	        		Column column = (Column) iter.next();
	        		getHtmlBuilder().append(column.getCellDisplay());
	        	}
	        }
	        getHtmlBuilder().trEnd(1);
	    }
	    private int getMultiColumn(){
	    	//決定要顯示的欄數
	    	String c = getTableModel().getPreferences().getPreference(UserPreferencesConstants.COLUMN_MULTI);
	    	if(c==null){
	    		return 1;
	    	}else{
	    		return Integer.valueOf(c);
	    	}
	    }
	    public void titleRowSpanColumns() {
	        boolean showTitle = BuilderUtils.showTitle(getTableModel());
	        if (showTitle) {
	            String title = getTableModel().getTableHandler().getTable().getTitle();
	            if (StringUtils.isNotBlank(title)) {
	                int columnCount = getTableModel().getColumnHandler().columnCount();
	                getHtmlBuilder().tr(1).styleClass(BuilderConstants.TITLE_ROW_CSS).close();
	                getHtmlBuilder().td(2).colSpan("" + columnCount*getMultiColumn()).close();
	                getHtmlBuilder().span().close().append(title).spanEnd();
	                getHtmlBuilder().tdEnd();
	                getHtmlBuilder().trEnd(1);
	            }
	        }
	    }	
	    public void filterRow() {
	        if (!getTableModel().getTableHandler().getTable().isFilterable()) {
	            return;
	        }

	        getHtmlBuilder().tr(1).styleClass(BuilderConstants.FILTER_CSS).close();

	        List columns = getTableModel().getColumnHandler().getFilterColumns();
	        //filter重複次數
	        for(int x = 0;x<getMultiColumn();x++){
	        for (Iterator iter = columns.iterator(); iter.hasNext();) {
	            Column column = (Column) iter.next();
	            getHtmlBuilder().append(column.getCellDisplay());
	        }
	        }
	        getHtmlBuilder().trEnd(1);
	    }
}
